package com.yuandian.customer.perception.controller;

import com.yuandian.api.customer_perception.po.BasePortraitParams;
import com.yuandian.bpm.common.core.util.R;
import com.yuandian.customer.perception.service.IUserPortraitService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

/**
 * 用户画像
 * @author liXin
 * @description 用户画像
 * @date 2022/4/24
 * @module yd-khgz-api
 */
@Slf4j
@RestController
@Api(value = "单位/用户画像", tags = "单位/用户画像")
@RequestMapping("/userPortrait")
public class UserPortraitController {

    @Autowired
	IUserPortraitService userPortraitService;


    /**
     *  操作员菜单访问量排行(TOP5)
     */
    @PostMapping("queryUserOperatorMenuVisitTop5")
    @ApiOperation(value = "操作员菜单访问量排行(TOP5)", notes = "操作员菜单访问量排行(TOP5)")
    public R<Object> queryUserOperatorMenuVisitTop5(@RequestBody BasePortraitParams params){
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        return userPortraitService.queryUserOperatorMenuVisitTop5(params);
    }

    /**
     *  操作员事项受理量排行(TOP5)
     */
    @PostMapping("queryUserOperatorProcessCountTop5")
    @ApiOperation(value = "操作员事项受理量排行(TOP5)", notes = "操作员事项受理量排行(TOP5)")
    public R<Object> queryUserOperatorProcessCountTop5(@RequestBody BasePortraitParams params){
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        //        默认按照成第一个步骤开始时间排序
        params.setOrderByColumn(Optional.ofNullable(params.getIsAsc()).orElse("asc"));
        return userPortraitService.queryUserOperatorProcessCountTop5(params);
    }

    /**
     *  事项平均空闲时长排行(TOP5)
     */
    @PostMapping("queryMatterAvgFreeTimeTop5")
    @ApiOperation(value = "事项平均空闲时长排行(TOP5)", notes = "事项平均空闲时长排行(TOP5)")
    public R<Object> queryMatterAvgFreeTimeTop5(@RequestBody BasePortraitParams params){
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        return userPortraitService.queryMatterAvgFreeTimeTop5(params);
    }

    /**
     *  事项平均办理时长排行(TOP5)
     */
    @PostMapping("queryMatterAvgProcessTimeTop5")
    @ApiOperation(value = "事项平均办理时长排行(TOP5)", notes = "事项平均办理时长排行(TOP5)")
    public R<Object> queryMatterAvgProcessTimeTop5(@RequestBody BasePortraitParams params){
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        return userPortraitService.queryMatterAvgProcessTimeTop5(params);
    }


    /**
     * 用户画像访问量，办理时长趋势图
     */
    @PostMapping("queryVisitAndDealTimeTrend")
    @ApiOperation(value = "用户画像访问量，办理时长趋势图", notes = "用户画像访问量，办理时长趋势图")
    public R<Object> queryVisitAndDealTimeTrend(@RequestBody BasePortraitParams params){
        if (params.getMatterIds()!=null&&params.getMatterIds().size()>0){
            List<String> matterIds=new ArrayList<>();
            params.getMatterIds().forEach(s -> {
                String matterId=s.split("@")[2];
                matterIds.add(matterId);
            });
            params.setMatterIds(matterIds);
        }
        return userPortraitService.queryVisitAndDealTimeTrend(params);
    }

}
